﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;

namespace ZG_member
{
    public partial class ReStoreForm : Form
    {
        public ReStoreForm()
        {
            InitializeComponent();
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            this.Opacity = 0;
        }

        private void btnAdd1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
            SqlCommand cmdBK = new SqlCommand();
            cmdBK.CommandType = CommandType.Text;
            cmdBK.Connection = conn;
            cmdBK.CommandText = @"backup database ZG_member to disk='D:/ZG_memberdatabase' with init";
           
            try
            {
                conn.Open();
                cmdBK.ExecuteNonQuery();
                MessageBox.Show("Backup successed.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            finally
            {
                conn.Close();

                conn.Dispose();
            }
        }

        private void btnfile_Click(object sender, EventArgs e)
        {
            string saveFileName = ""; 
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Filter = "文本文件(*.bak)|*.txt|所有文件(*.*)|*.*";
            saveDialog.FilterIndex = 2;
            saveDialog.FileName = "数据备份";
            saveDialog.ShowDialog();
            saveFileName = saveDialog.FileName;
            textBox1.Text = saveFileName;
        }

        private void btnfile2_Click(object sender, EventArgs e)
        {

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "文本文件(*.bak)|*.txt|所有文件(*.*)|*.*";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                FileInfo fileInfo = new FileInfo(openFileDialog.FileName);
                string filePath = fileInfo.FullName;
                //textBox2.Text = filePath;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            MessageBox.Show("该功能暂时不能用!!"); return;
           
            string aa = ConfigurationManager.AppSettings["ConnectionString"];
            aa = aa.Replace("database=ZG_member", "database=master;Trusted_Connection=False");
            MessageBox.Show(aa);
            SqlConnection conn = new SqlConnection(aa);
            try
            {
                conn.Open();
                SqlCommand cmdRT = new SqlCommand();
                cmdRT.CommandType = CommandType.Text;
                cmdRT.Connection = conn;
                cmdRT.CommandText = @"restore database ZG_member from disk='D:/ZG_memberdatabase'";
                cmdRT.ExecuteNonQuery();
                MessageBox.Show("Restore successed.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                conn.Close();
            }


        }

        private void tabPage1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "D:/ZG_memberdatabase";
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {
            //textBox2.Text = "D:/ZG_memberdatabase";
           
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }

        private void ReStoreForm_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
        }

        private void btncancel1_Click(object sender, EventArgs e)
        {
            this.timer2.Start();
        }
        int flag = 0;
        private void ReStoreForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void btncancel1_Click_1(object sender, EventArgs e)
        {
            this.timer2.Start();
        }


    }
}